--
-- Author: mm
-- Date: 2018-03-19 14:55:37
--
-- NewguildMemberRender 对应 Node_player_list.csb
-- 工会成员列表render

local NewguildMemberHeadRender = import(".NewguildMemberHeadRender")

local ClassRef = app.mvc.createListViewCell(...)
local string_format = string.format
--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewGuildMemberNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)
    self:setContentSize(self._ui.Panel:getContentSize())
    self._memberHead = NewguildMemberHeadRender.create( self._ui.head )

    display.uiAddClick(self._ui.btn_num, function()
        local posTemp = self._ui.btn_num:convertToWorldSpace(cc.p(0,0))
        local width, height = self._ui.btn_num:getContentSize2()
        posTemp.y = posTemp.y + height
        self:dispatchCellEvent("preview_guildMemberInfo", { self._data, posTemp })
    end)

    self._ui.Button_more:addClickEventListener(function()
        local pos = self._ui.Panel:convertToWorldSpace( self._ui.Button_more:getPosition() )
        pos.x = pos.x + 30
        self:dispatchCellEvent("guildMember_btnMore", {attr=self._data, pos=pos})
        --executeFunc(self._onClick, "guildMember_btnMore", {attr=self._data, pos=pos})
    end)
    self._ui.Button_m:addClickEventListener(function()
        if not UD:isMe( self._data:getMemberId() ) then -- 不是我自己？
            if self._data:isOnline() then --在线
                self:dispatchCellEvent("wshisper_guildMemberInfo", self._data)
                --executeFunc( self._onClick, "wshisper_guildMemberInfo", self._data )
            else
                display.pushToast(L("tips_1050011"))
            end
        else
            display.pushToast( L("tips_3000327") )
        end
    end)
    self._ui.Button_t:addClickEventListener(function()
        if not UD:isMe( self._data:getMemberId() ) then
            self:dispatchCellEvent("team_guildMemberInfo", self._data)
            --executeFunc( self._onClick, "team_guildMemberInfo", self._data )
        else
            display.pushToast( L("tips_3000327") )
        end
    end)

    self._ui.Panel_leave:setTouchEnabled(true)
    display.uiAddClick(self._ui.Panel_leave, function()
        local posTemp = self._ui.Panel_leave:convertToWorldSpace(cc.p(0,0))
        local width = self._ui.Panel_leave:getContentSize2()
        posTemp.x = posTemp.x + width

        local maxLeaveDay = tonumber(GD:getSystemPar("guildposition1AfkMax"))
        local haveLeaveDay = self._data:getOfflineDay()
        local tipsStr = string_format(L"guild_position1_change2",haveLeaveDay,maxLeaveDay)

        self:dispatchCellEvent("showTips_challenge", { L("guild_position1_change1"), tipsStr, posTemp })
    end)

    local loadingbar = self._ui.Panel_leave:getChildByName("LoadingBar")
    loadingbar:setVisible(false)
    if not self._loadingbar then
        --dump(loadingbar)
        self._loadingbar = cc.ProgressTimer:create( loadingbar )
        self._loadingbar:setType( cc.PROGRESS_TIMER_TYPE_BAR )
        self._loadingbar:setScale(1)
        self._loadingbar:setMidpoint(cc.p( 0.5, 0 ))
        self._loadingbar:setBarChangeRate(cc.p(0, 1));
        self._loadingbar:setPosition(loadingbar:getPosition())
        self._ui.Panel_leave:addChild(self._loadingbar)
    end
    self:refresh()
end

-- data = guildMemberVo 结构
function ClassRef:refresh()
    if self._data then
        self:setVisible( true )
        local data = self._data

        self._memberHead:setData( data )

        if self._maxActiveTerritoryId and self._maxActiveTerritoryId > 0 then
            if data:isGuildManager() then
                self._ui.icon_grade:loadTexture(string.format("%s%s%d.png", Res.guildPositionIconPath,"guild_guanzhi_bg",2))
            else
                self._ui.icon_grade:loadTexture(string.format("%s%s%d.png", Res.guildPositionIconPath,"guild_guanzhi_bg",data:getPosition()))
            end
        else
            self._ui.icon_grade:loadTexture(string.format("%s%s%d.png", Res.guildPositionIconPath,"guild_guanzhi_bg",data:getPosition())) -- 身份图标底图
        end

        self._ui["Panel/icon_grade/icon"]:loadTexture( data:getPositionIcon() ) -- 身份图标

        self._ui.btn_num:getChildByName( "num" ):setString( data:getDonateLv() ) -- 捐献等级
        self._ui.mask:setVisible( not data:isOnline() ) -- 是否在线
        --如果是会长 一定时间没有上线

        if data:isGuildLeader() then
            local leaveDay = tonumber(GD:getSystemPar("guildposition1AfkMin"))
            local maxLeaveDay = tonumber(GD:getSystemPar("guildposition1AfkMax"))
            local haveLeaveDay = data:getOfflineDay()
            local isOnline = data:isOnline()
            --print(haveLeaveDay,leaveDay)
            if not isOnline and haveLeaveDay >= leaveDay then
                self._ui.Panel_leave:setVisible(true)
                self._loadingbar:setPercentage(haveLeaveDay/maxLeaveDay * 100)
            else
                self._ui.Panel_leave:setVisible(false)
            end
        else
            self._ui.Panel_leave:setVisible(false)
        end

    else
        self:setVisible( false )
    end
end

--公会目前最大激活的爵位ID
function ClassRef:setMaxActiveTerritoryId(maxActiveTerritoryId)
    self._maxActiveTerritoryId = maxActiveTerritoryId
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
